<?php
	class noidungtindb
	{
		public function Add($MaBaiViet,
							$MaChuDe,
							$MaNguoiDung,
							$TieuDe,
							$TomTat,
							$NoiDung,
							$HinhAnh,
							$TenAnh,
							$NgayDang,
							$LuotXem,
							$KiemDuyet)
		{
			$sql = "INSERT INTO 
					`tbl_noidungtin`(`MaChuDe`, `MaNguoiDung`, `TieuDe`, `TomTat`, `NoiDung`, `HinhAnh`, `TenAnh`, `NgayDang`, `LuotXem`, `KiemDuyet`) 
					VALUES ($MaChuDe,$MaNguoiDung,".dataservice::slashes($TieuDe).",".dataservice::slashes($TomTat).",".dataservice::slashes($NoiDung).",".dataservice::slashes($HinhAnh).", ".dataservice::slashes($TenAnh).", now(), $LuotXem, $KiemDuyet)";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);		
		}
		
		public function Update($MaBaiViet, $MaChuDe, $TieuDe, $TomTat, $NoiDung, $HinhAnh, $TenAnh)
		{
			if($HinhAnh != "")
				$sql = "UPDATE `tbl_noidungtin` 
						SET `MaChuDe`=$MaChuDe,
						`TieuDe`=".dataservice::slashes($TieuDe).",
						`TomTat`=".dataservice::slashes($TomTat).",
						`NoiDung`=".dataservice::slashes($NoiDung).",
						`HinhAnh`=".dataservice::slashes($HinhAnh).",
						`TenAnh`=".dataservice::slashes($TenAnh)."
						WHERE `MaBaiViet`=$MaBaiViet";
			else
				$sql = "UPDATE `tbl_noidungtin` 
						SET `MaChuDe`=$MaChuDe,
						`TieuDe`=".dataservice::slashes($TieuDe).",
						`TomTat`=".dataservice::slashes($TomTat).",
						`NoiDung`=".dataservice::slashes($NoiDung).",
						`TenAnh`=".dataservice::slashes($TenAnh)."
						WHERE `MaBaiViet`=$MaBaiViet";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Delete($MaBaiViet)
		{
			$sql = "DELETE FROM `tbl_noidungtin` WHERE `MaBaiViet` = $MaBaiViet";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function GetAllList($MaBaiViet)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe";
			if($MaBaiViet != 0)
				$sql.= " AND tbl_noidungtin.MaBaiViet = $MaBaiViet";
			$sql.= " ORDER BY tbl_noidungtin.NgayDang DESC";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetAllListCheck($KiemDuyet)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe";
			$sql.= " AND tbl_noidungtin.KiemDuyet = $KiemDuyet";
			$sql.= " ORDER BY tbl_noidungtin.NgayDang DESC";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function SortGetAllList()
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe
					AND tbl_noidungtin.KiemDuyet = 1 ORDER BY tbl_noidungtin.NgayDang DESC";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function SortGetPageList($start, $limit)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe
					AND tbl_noidungtin.KiemDuyet = 1 ORDER BY tbl_noidungtin.NgayDang DESC LIMIT $start, $limit";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function SortGetCatalogPageList($MaChuDe, $start, $limit)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe
					AND tbl_noidungtin.KiemDuyet = 1 AND tbl_chude.MaChuDe = $MaChuDe ORDER BY tbl_noidungtin.NgayDang DESC LIMIT $start, $limit";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function SortGetCatalogList($MaChuDe)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe
					AND tbl_noidungtin.KiemDuyet = 1 AND tbl_chude.MaChuDe = $MaChuDe ORDER BY tbl_noidungtin.NgayDang DESC";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function SortGetView()
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe
					AND tbl_noidungtin.KiemDuyet = 1 ORDER BY tbl_noidungtin.LuotXem DESC LIMIT 0, 9";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function AscNumView($MaBaiViet)
		{
			$sql = "UPDATE `tbl_noidungtin` SET `LuotXem` = `LuotXem` + 1 WHERE `MaBaiViet` = $MaBaiViet";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CheckArticle($MaBaiViet, $KiemDuyet)
		{
			$sql = "UPDATE `tbl_noidungtin` SET `KiemDuyet` = $KiemDuyet WHERE `MaBaiViet` = $MaBaiViet";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Find($TieuDe, $start, $limit)
		{
			$sql = "SELECT * 
					FROM tbl_noidungtin, tbl_nguoidung, tbl_chude 
					WHERE tbl_noidungtin.MaNguoiDung = tbl_nguoidung.MaNguoiDung AND tbl_noidungtin.MaChuDe = tbl_chude.MaChuDe";
			if($TieuDe != "")
				$sql.= " AND tbl_noidungtin.TieuDe LIKE '%$TieuDe%'";
			$sql.= " ORDER BY tbl_noidungtin.NgayDang DESC LIMIT $start, $limit";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
	}
?>